package cn.wetcland.urban.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.wetcland.urban.admin.dto.NoticeAddDTO;
import cn.wetcland.urban.admin.dto.NoticeUpdateDTO;
import cn.wetcland.urban.admin.mapper.UtNoticeMapper;
import cn.wetcland.urban.admin.service.UtNoticeService;
import cn.wetcland.urban.common.MyAssert;
import cn.wetcland.urban.common.utils.CopyObjectUtil;
import cn.wetcland.urban.generator.domain.UtNotice;
import cn.wetcland.urban.generator.domain.UtOrder;
import cn.wetcland.urban.generator.dto.BaseConditionSearchDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author Wetcland
* @description 针对表【ut_notice】的数据库操作Service实现
* @createDate 2024-01-21 09:51:59
*/
@Service
public class UtNoticeServiceImpl extends ServiceImpl<UtNoticeMapper, UtNotice> implements UtNoticeService {

    @Resource
    private UtNoticeMapper noticeMapper;

    @Override
    public Page<UtNotice> noticeList(BaseConditionSearchDTO searchDTO) {
        Page<UtNotice> noticePage = new Page<>(searchDTO.getCurPage(), searchDTO.getPageSize());
        return noticeMapper.selectNoticeListPage(noticePage, searchDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void noticeUpdate(NoticeUpdateDTO updateDTO) {
        UtNotice utNotice = noticeMapper.selectById(updateDTO.getNoticeId());
        MyAssert.notNull(utNotice, "该通知不存在");
        utNotice.setNoticeTitle(updateDTO.getNoticeTitle());
        utNotice.setNoticeContent(updateDTO.getNoticeContent());
        utNotice.setPriority(updateDTO.getPriority());
        utNotice.setFace(updateDTO.getFace());
        noticeMapper.updateById(utNotice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void noticeDelete(Long noticeId) {
        noticeMapper.deleteById(noticeId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void noticeAdd(NoticeAddDTO addDTO) {
        UtNotice utNotice = new UtNotice();
        CopyObjectUtil.copyPropertiesIgnoreNull(addDTO, utNotice);
        utNotice.setPublishAdminId(StpUtil.getLoginIdAsLong());
        noticeMapper.insert(utNotice);
    }
}




